package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Part;
import com.sdry.model.hpf.Product;
import com.sdry.service.hpf.BomService;
import com.sdry.service.hpf.PartService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
 * BOM管理控制层
* @author lian 
* @version 1.0
* @time 2018年10月15日 下午2:49:56
*/

@Controller
@RequestMapping("/bomMana")
public class BomController {

	@Resource
	private BomService bomService;  //bom管理业务层
	@Resource
	private PartService partService;  //注入配件service
	
	/**
	 * 分页查询产品信息
	 * @param page 分页条件
	 * @param product 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageBom")
	public void listPageBom(Page page, Product product, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (product.getPname() != null ) {
			map.put("pname", product.getPname());
		} else {
			map.put("pname", "");
			product.setPname("");
		}
		int count = bomService.countBom(product);
		List<Product> BomList = bomService.listPageBom(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(BomList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据产品ID分页查询产品的配件信息
	 * @param page 分页条件
	 * @param product 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPagePartByProduct")
	public void listPagePartByProduct(Page page, Product product, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		map.put("productId", product.getPid());
		int count = bomService.countPartByProduct(product);
		List<Part> partList = bomService.listPagePartByProduct(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(partList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * 获取所有配件信息
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listAllPart")
	public void listAllPart(HttpServletResponse response) throws Exception{
		Part part = new Part();
		part.setPanum("");
		List<Part> partList = bomService.listAllPart();
		int count = partService.countPart(part);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(partList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存BOM信息
	 * @param bom BOM信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveBom")
	public void saveBom(Bom bom, HttpServletResponse response) throws Exception{
		 int isSuccess = bomService.insertBom(bom);
		 if (isSuccess > 0) {
			 ResponseUtil.write(response, isSuccess);
		} else {
			ResponseUtil.write(response, "");
		}
	}
	
	/**
	 * 删除BOM信息
	 * @param bom BOM信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteBom")
	public void deleteBom(Bom bom, HttpServletResponse response) throws Exception{
		 int isSuccess = bomService.deleteBom(bom);
		 if (isSuccess > 0) {
			 ResponseUtil.write(response, isSuccess);
		} else {
			ResponseUtil.write(response, "");
		}
	}
}
